package com.leon.alishop.core.config.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.leon.alishop.core.config.ProviderConstant;
import com.leon.alishop.dao.ProviderInfoDao;
import com.leon.alishop.model.ProviderInfo;

public class ProviderConstantImpl extends ProviderConstant {

	private ProviderInfoDao providerInfoDao;
	
	private List<ProviderInfo> providerInfoList;
	private Map<Integer,ProviderInfo> providerInfoMap;
	
	@Override
	public List<ProviderInfo> getProviderInfoList() {
		List<ProviderInfo> list = providerInfoList;
		if(list == null){
			list = providerInfoDao.findAllProviderInfo(null,null,0, 1000);
			providerInfoList = list;
		}
		return new ArrayList<ProviderInfo>(list);
	}

	@Override
	public void reload() {
		providerInfoList = null;
		providerInfoMap = null;
	}

	public void setProviderInfoDao(ProviderInfoDao providerInfoDao) {
		this.providerInfoDao = providerInfoDao;
	}

	@Override
	public ProviderInfo getProviderInfoById(Integer providerId) {
		Map<Integer,ProviderInfo> map = providerInfoMap;
		if(map == null){
			map = new HashMap<Integer, ProviderInfo>();
			List<ProviderInfo> list = this.getProviderInfoList();
			if(null != list){
				for (ProviderInfo providerInfo : list) {
					map.put(providerInfo.getId(), providerInfo);
				}
			}
			providerInfoMap = map;
		}
		return map.get(providerId);
	}

}
